<?php
/**
 * 短信&语音验证码
 */
class Yuntongxun {
	
	/**
     * 取得Yuntongxun实例
     * @static
     * @return mixed 返回Yuntongxun
     */
    public static function getInstance() {
        return new Yuntongxun();
    }
 
	/**
	 * 发送短信验证码
	 * @params string $tel 手机号(多个用逗号分隔)
	 * @param array $datas 内容数据 格式为数组
	 * @params int $tempId 模板ID
	 */
	public function sendSMS($tel,$datas,$tempId){
	    if($tel=='' or !My::is_id($tempId) or !$datas)
	        return false;
	    $telArr = My::explode(',',$tel);
	    foreach($telArr as $key=>&$value){
	        if(!My::is_tel($value))
	            unset($telArr[$key]);
	    }
	    if(!$telArr)
	        return false;
	    require_once (COM_DIR."Sms/yuntongxun/CCPRestSDK.php");
   
	    $rest = new REST(C('sms_config.server_ip'),C('sms_config.server_port'),C('sms_config.soft_version'));
	    $rest->setAccount(C('sms_config.account_sid'),C('sms_config.account_token'));
	    $rest->setAppId(C('sms_config.app_id'));
            
        // 发送模板短信
        $result = $rest->sendTemplateSMS($tel, $datas, $tempId);
        if ($result == NULL) {
            return false;//
        }
        if ($result->statusCode != 0) {
            //echo "error code :" . $result->statusCode . "<br>";
            //echo "error msg :" . $result->statusMsg . "<br>";
            // TODO 添加错误处理逻辑
            return false;
        } else {
            // 获取返回信息
            //$smsmessage = $result->TemplateSMS;
            //echo "dateCreated:" . $smsmessage->dateCreated . "<br/>";
            //echo "smsMessageSid:" . $smsmessage->smsMessageSid . "<br/>";
            // TODO 添加成功处理逻辑
            return true;
        }
    }
    
    
    /**
     * 发送语音验证码
     * @params string $tel 手机号
     * @param string $code 验证码
     */
    public function sendVoice($tel,$code){
        if(!My::is_tel($tel) or !$code)
            return false;
        require_once (COM_DIR."Sms/yuntongxun/CCPRestSDK.php");
         
        $rest = new REST(C('sms_config.server_ip'),C('sms_config.server_port'),C('sms_config.soft_version'));
        $rest->setAccount(C('sms_config.account_sid'),C('sms_config.account_token'));
        $rest->setAppId(C('sms_config.app_id'));
    
        $displayNum = '';//显示的主叫号码
        // 发送语音
        $result = $rest->voiceVerify($code,2,$tel,$displayNum,'','zh','');
        if ($result == NULL) {
            return false;//
        }
        if ($result->statusCode != 0) {
            //echo "error code :" . $result->statusCode . "<br>";
            //echo "error msg :" . $result->statusMsg . "<br>";
            // TODO 添加错误处理逻辑
            return false;
        } else {
            // 获取返回信息
            //$smsmessage = $result->TemplateSMS;
            //echo "dateCreated:" . $smsmessage->dateCreated . "<br/>";
            //echo "smsMessageSid:" . $smsmessage->smsMessageSid . "<br/>";
            // TODO 添加成功处理逻辑
            return true;
        }
    }
	
	
}